import _get from 'lodash/get';

/**
 * 名字与id 映射,
 * @param list 数组
 * @param key  键值 (`id`, `user.name`) 可以链式获取
 * @param prefix 属性前缀 
 * @param afterfix 属性后缀
 */

export function valueMapToKey(list: any, key: string, prefix = '', afterfix = '') {
    let result = {};
    list.map((item: any) => {
        result = {
            ...result,
            [`${prefix}${_get(item, key)}${afterfix}`]: item,
        }
    })

    return result;
}

/**
 * 
 * @param fileName 文件名
 * @param data 数据
 */

export function download(fileName: string, data: string) {
    let obj: any = new Blob([data]);
    let elem = document.createElement('a');
    elem.download = fileName || '下载';
    elem.href = URL.createObjectURL(obj);
    elem.click();
    setTimeout(function() {
        URL.revokeObjectURL(obj);
    }, 100);
}